package com.sf.sgs.sessionevaluate.controller;

import java.io.Serializable;
import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sf.sgs.sessionevaluate.controller.dto.req.SessionInfoReq;
import com.sf.sgs.sessionevaluate.controller.response.RestResponse;
import com.sf.sgs.sessionevaluate.domain.SessionInfo;
import com.sf.sgs.sessionevaluate.service.ISessionInfoService;

@Controller
@RequestMapping("sessionInfos")
public class SessionInfoController extends BaseController {

	@Autowired
	private ISessionInfoService sessionInfoService;

	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public RestResponse<? extends Serializable> saveInfo(@RequestBody SessionInfoReq sessionInfoReq) {

		RestResponse<SessionInfo> response = handle(new BaseController.Action<SessionInfo>() {

			@Override
			public void onHandle(RestResponse<SessionInfo> restResponse) {
				SessionInfo sessionInfo = new SessionInfo();
				BeanUtils.copyProperties(sessionInfoReq, sessionInfo);
				sessionInfo.setCreateTime(new Date());
				sessionInfoService.save(sessionInfo);

				restResponse.setResult(sessionInfo);
			}

		});

		return response;

	}

	@RequestMapping(value = "/evaluates/{id}", method = RequestMethod.GET)
	public ModelAndView getEvaluate(@PathVariable(value = "id") Long id) {
		SessionInfo sessionInfo = sessionInfoService.get(id);
		ModelAndView modelAndView = new ModelAndView("evaluate", "sessionInfo", sessionInfo);
		return modelAndView;
	}

	@RequestMapping(value = "/success/{id}", method = RequestMethod.GET)
	public ModelAndView getSuccess(@PathVariable(value = "id") Long id) {
		ModelAndView modelAndView = new ModelAndView("sessionSuccess", "id", id);
		return modelAndView;
	}

	@RequestMapping(path = "/evaluate")
	public String addSession() {
		return "session";
	}

}
